package com.hckjsec.website.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.website.domain.WebSiteMechanism;
import com.hckjsec.website.service.IWebSiteMechanismService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 机构简介Controller
 * 
 * @author cyt
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/webmechanism")
public class WebSiteMechanismController extends BaseController
{
    @Autowired
    private IWebSiteMechanismService webSiteMechanismService;


    /**
     * 获取机构简介信息
     */
    @GetMapping("/getIntroduce")
    public AjaxResult getIntroduce()
    {
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        WebSiteMechanism webSiteMechanism = new WebSiteMechanism();
        webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
        webSiteMechanism.setType("introduce");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteMechanism);
        if (!StringUtils.isEmpty(list)){
            webSiteConfig = list.get(0);
        }
        return AjaxResult.success(webSiteConfig);
    }
    /**
     * 获取组织架构信息
     */
    @GetMapping("/getFramework")
    public AjaxResult getFramework()
    {
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        WebSiteMechanism webSiteMechanism = new WebSiteMechanism();
        webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
        webSiteMechanism.setType("framework");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteMechanism);
        if (!StringUtils.isEmpty(list)){
            webSiteConfig = list.get(0);
        }
        return AjaxResult.success(webSiteConfig);
    }
    /**
     * 获取领导致辞信息
     */
    @GetMapping("/getAddress")
    public AjaxResult getAddress()
    {
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        WebSiteMechanism webSiteMechanism = new WebSiteMechanism();
        webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
        webSiteMechanism.setType("address");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteMechanism);
        if (!StringUtils.isEmpty(list)){
            webSiteConfig = list.get(0);
        }
        return AjaxResult.success(webSiteConfig);
    }

    /**
     * 获取联系我们信息
     */
    @GetMapping("/getContactUs")
    public AjaxResult getContactUs()
    {
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        WebSiteMechanism webSiteMechanism = new WebSiteMechanism();
        webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
        webSiteMechanism.setType("contactus");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteMechanism);
        if (!StringUtils.isEmpty(list)){
            webSiteConfig = list.get(0);
        }
        return AjaxResult.success(webSiteConfig);
    }

    /**
     * 新增机构简介信息
     */
    @Log(title = "机构简介", businessType = BusinessType.INSERT)
    @PostMapping(value = "/setIntroduce")
    public AjaxResult setIntroduce(@RequestBody WebSiteMechanism webSiteMechanism)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteMechanism.getContent(),""))){//解密
            webSiteMechanism.setContent(Base64Converter.decode(webSiteMechanism.getContent()));
        }else{
            webSiteMechanism.setContent(webSiteMechanism.getContent());
        }
        webSiteMechanism.setOperator(SecurityUtils.getUserId());
        int rows = 0;
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        webSiteConfig.setDeptId(SecurityUtils.getDeptId());
        webSiteConfig.setType("introduce");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteConfig);
        if (!StringUtils.isEmpty(list)){
            rows =  webSiteMechanismService.updateWebSiteMechanism(webSiteMechanism);
        }else{
            webSiteMechanism.setType("introduce");
            webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
            rows =  webSiteMechanismService.insertWebSiteMechanism(webSiteMechanism);
        }
        return toAjax(rows);
    }

    /**
     * 新增组织架构信息
     */
    @Log(title = "组织架构", businessType = BusinessType.INSERT)
    @PostMapping(value = "/setFramework")
    public AjaxResult setFramework(@RequestBody WebSiteMechanism webSiteMechanism)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteMechanism.getContent(),""))){//解密
            webSiteMechanism.setContent(Base64Converter.decode(webSiteMechanism.getContent()));
        }else{
            webSiteMechanism.setContent(webSiteMechanism.getContent());
        }
        webSiteMechanism.setOperator(SecurityUtils.getUserId());
        int rows = 0;
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        webSiteConfig.setDeptId(SecurityUtils.getDeptId());
        webSiteConfig.setType("framework");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteConfig);
        if (!StringUtils.isEmpty(list)){
            rows =  webSiteMechanismService.updateWebSiteMechanism(webSiteMechanism);
        }else{
            webSiteMechanism.setType("framework");
            webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
            rows =  webSiteMechanismService.insertWebSiteMechanism(webSiteMechanism);
        }
        return toAjax(rows);
    }

    /**
     * 新增领导致辞信息
     */
    @Log(title = "领导致辞", businessType = BusinessType.INSERT)
    @PostMapping(value = "/setAddress")
    public AjaxResult setAddress(@RequestBody WebSiteMechanism webSiteMechanism)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteMechanism.getContent(),""))){//解密
            webSiteMechanism.setContent(Base64Converter.decode(webSiteMechanism.getContent()));
        }else{
            webSiteMechanism.setContent(webSiteMechanism.getContent());
        }
        webSiteMechanism.setOperator(SecurityUtils.getUserId());
        int rows = 0;
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        webSiteConfig.setDeptId(SecurityUtils.getDeptId());
        webSiteConfig.setType("address");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteConfig);
        if (!StringUtils.isEmpty(list)){
            rows =  webSiteMechanismService.updateWebSiteMechanism(webSiteMechanism);
        }else{
            webSiteMechanism.setType("address");
            webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
            rows =  webSiteMechanismService.insertWebSiteMechanism(webSiteMechanism);
        }
        return toAjax(rows);
    }

    /**
     * 新增联系我们信息
     */
    @Log(title = "联系我们", businessType = BusinessType.INSERT)
    @PostMapping(value = "/setContactUs")
    public AjaxResult setContactUs(@RequestBody WebSiteMechanism webSiteMechanism)
    {
        if (!"".equals(StringUtils.isHcNull(webSiteMechanism.getContent(),""))){//解密
            webSiteMechanism.setContent(Base64Converter.decode(webSiteMechanism.getContent()));
        }else{
            webSiteMechanism.setContent(webSiteMechanism.getContent());
        }
        webSiteMechanism.setOperator(SecurityUtils.getUserId());
        int rows = 0;
        WebSiteMechanism webSiteConfig = new WebSiteMechanism();
        webSiteConfig.setDeptId(SecurityUtils.getDeptId());
        webSiteConfig.setType("contactus");
        List<WebSiteMechanism> list = webSiteMechanismService.selectWebSiteMechanismList(webSiteConfig);
        if (!StringUtils.isEmpty(list)){
            rows =  webSiteMechanismService.updateWebSiteMechanism(webSiteMechanism);
        }else{
            webSiteMechanism.setType("contactus");
            webSiteMechanism.setDeptId(SecurityUtils.getDeptId());
            rows =  webSiteMechanismService.insertWebSiteMechanism(webSiteMechanism);
        }
        return toAjax(rows);
    }
}
